<template>
	<view>
		<u-popup :show="show" mode="center" :overlay="true" :popup="false" :closeable="false"
			:closeOnClickOverlay="true" :safeAreaInsetBottom="true" :zIndex="zIndex" :round="20"
			close-icon-color="#2B85E4" @close="success">
			<view class="u-p-20">
				<view class="u-p-10">微信小程序登录</view>
				<view class="u-p-40 u-m-t-40 u-text-center" style="width:80vw;">
					<u-icon name="weixin-fill" color="#1aad19" size="220"></u-icon>
					<button type="primary" class="u-m-t-40" @click="getUserInfo">微信授权登录</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		reLogin,
		postLogin,
		postRegister,
		getOpenIdByCode,
		loginByPhone
	} from '@/config/api.js'

	/**
	 * m-login-wxapp 小程序登录组件
	 * @property {string} success 成功后返回的数据
	 * @property {String} z-index 弹出时的z-index值
	 */
	export default {
		name: 'm-login',
		props: {
			zIndex: {
				type: [String, Number],
				default: 0
			}
		},
		data() {
			return {
				code: '',
				user: {},
				show: false,
				resolve: null,
				reject: null
			};
		},
		mounted() {

		},
		methods: {
			wx_login() {
				wx.login({
					success: (res) => {
						this.code = res.code
					}
				})
			},
			// 本地用户数据
			local() {

				let UserInfo = uni.getStorageSync("UserInfo");

				if (UserInfo) {
					this.user = UserInfo;
					return this.success();
				} else {
					this.user = {};
					return this.checkLogin();
				}
			},
			// 重载用户数据
			reload() {
				reLogin().then((ret) => {
					return this.success();
				}).catch((err) => {
					return this.checkLogin();
				});
			},
			// 登录成功返回
			success() {
				this.show = false;
				this.$emit('success', this.user);
			},
			error(data) {
				this.$emit('error', data);
			},
			// 用户静默登录
			checkLogin() {
				let that = this;
				uni.login({
					success(ret) {
						if (ret.code) {
							that.code = ret.code;
							postLogin({
								code: that.code
							}).then(res => {
								if (res.id) {
									uni.setStorageSync('UserInfo', res || {});
									uni.setStorageSync('UserToken', res.token.token || '');
									that.success();
								} else {
									// that.show = true;
								}
							}).catch(err => {
								uni.clearStorage();
								that.error(err)
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '调用微信授权失败！',
								duration: 2000
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '调用微信授权失败！',
							duration: 2000
						});
					}
				});
			},
			// 小程序授权登录
			getUserInfo(e) {
				let that = this;
				uni.getUserProfile({
					lang: 'zh_CN',
					desc: '用于完善用户资料',
					fail(res) {
						console.log(res)
						if (res.errMsg === 'getUserProfile:fail auth deny') {
							uni.showToast({
								icon: 'none',
								title: '您取消了微信授权！',
								duration: 2000
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '调用微信授权失败！',
								duration: 2000
							});
						}
					},
					success(res) {
						let data = {
							iv: res.iv,
							code: that.code,
							encrypted: res.encryptedData,
							from: uni.getStorageSync('ivcode')
						};
						postRegister(data).then(ret => {
							uni.setStorageSync('UserInfo', ret || {});
							uni.setStorageSync('UserToken', ret.token.token || '');
							that.user = ret;
							uni.showToast({
								icon: 'none',
								title: '授权登录成功！',
								duration: 2000
							});
							that.success();
						}).catch((err) => {
							that.error();
							uni.showToast({
								icon: 'none',
								title: err.info,
								duration: 2000
							});
						});
					}
				});
			},

			doLoginByPhone(e) {
				let that = this;
				var open_id = uni.getStorageSync('openID');
				
				let data = {
					open_id: open_id,
					code: e.detail.code,
					encrypted: e.detail.encryptedData,
					iv: e.detail.iv,
					from: uni.getStorageSync('ivcode')
				};

				loginByPhone(data).then(res => {
					uni.setStorageSync('UserInfo', res || {});
					uni.setStorageSync('UserToken', res.token.token || '');
					that.user = res;
					uni.showToast({
						icon: 'none',
						title: '授权登录成功！',
						duration: 2000
					});
					that.success();
				}).catch((err) => {
					console.log(err)
					that.error();
					uni.showToast({
						icon: 'none',
						title: err.info,
						duration: 2000
					});
				});
			},

			// 获取Openid
			getOpenId(e) {
				let that = this;
				uni.login({
					success(ret) {
						if (ret.code) {
							that.code = ret.code;
							getOpenIdByCode({
								code: that.code
							}).then(res => {
								if (res.open_id) {
									uni.setStorageSync('openID', res.open_id || {});
								} else {
									// that.show = true;
								}
							}).catch(err => {});
						} else {
							uni.showToast({
								icon: 'none',
								title: '调用微信授权失败！',
								duration: 2000
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '调用微信授权失败！',
							duration: 2000
						});
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped></style>
